package main

import (
	"flag"
	log "github.com/micro/go-micro/v2/logger"
	"github.com/micro/go-micro/v2/registry"
	"github.com/micro/go-micro/v2/registry/etcd"
	"github.com/micro/go-micro/v2/web"
	"web-service/api/config"
	"web-service/api/middleware"
	"web-service/api/router"
)

var (
	//指定配置文件路径,默认使用当前目录下的config.ini文件
	configPath = flag.String("config", "./config.ini", "-config file path")
)

func InitServer() {
	//解析命令运行参数
	flag.Parse()
	//从config.json加载配置信息
	log.Info("Config Path:", *configPath)
	//解析配置文件
	config.InitConfig(*configPath)
}

func main() {
	InitServer()
	conf := config.GetConfig()

	//定义gin router
	ginRouter := router.InitGinRouter()
	registry := etcd.NewRegistry(registry.Addrs(conf.RegistryAddr))

	//接收http请求,因此创建一个webService类型的服务,用gin框架做为路由
	httpServer := web.NewService(
		web.Address(":"+conf.Port),
		web.Handler(ginRouter),
		web.Name(conf.ServiceName),
		web.Version(conf.Version),
		web.Registry(registry),
	)

	if err := httpServer.Init(); err != nil {
		log.Fatal(err)
	}

	//创建RPC调用client
	middleware.RegisterRpcClient()

	// Run service
	if err := httpServer.Run(); err != nil {
		log.Fatal(err)
	}
}
